import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 给你一个单链表的头节点 head ，请你判断该链表是否为回文链表。如果是，返回 true ；否则，返回 false 。
 * <p>
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        if (Objects.isNull(head)) {
            return true;
        }

        List<Integer> list = new ArrayList<>();
        list.add(head.val);
        while (Objects.nonNull(head.next)) {
            head = head.next;
            list.add(head.val);
        }

        int start = 0;
        int end = list.size() - 1;
        while (end > start) {
            if (!list.get(start).equals(list.get(end))) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }

}